﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DAL;
using HCSExtensions;
using System.Configuration;
using System.Net;
using System.IO;
using Dynamics.Enums;

namespace BLL
{
    public class ProductProvider : BaseProvider
    {
        private const int FolderImageLimit = 1000;

        public TransactionEvent SaveProduct(Product product)
        {
            TransactionEvent transaction = new TransactionEvent();

            Product oldProduct = base.Context.Products.FirstOrDefault(t => t.SiteID == product.SiteID && t.SiteProductID == product.SiteProductID);
            if (oldProduct != null)
            {
                oldProduct.Price = product.Price;
                oldProduct.OldPrice = product.OldPrice;
                oldProduct.DiscountPercent = product.DiscountPercent;
                oldProduct.DiscountPrice = product.DiscountPrice;
                return this.Update(oldProduct);
            }

            product.ImageUrl = DownloadImage(product);
            return this.Add(product);
        }

        public string DownloadImage(Product product)
        {
            try
            {
                int imageCount = base.Context.Products.Count(t => !string.IsNullOrEmpty(t.ImageUrl));
                if (imageCount > FolderImageLimit)
                {
                    imageCount /= FolderImageLimit;
                }
                else
                {
                    imageCount = 1;
                }

                string imagesPath = Path.Combine(ConfigurationManager.AppSettings.Get("ImagesPath"), imageCount.ToString());
                if (!Directory.Exists(imagesPath))
                {
                    Directory.CreateDirectory(imagesPath);
                }

                string fileName = StringManager.GetRandomNumber(10000, 99999999).ToString() + "_" + DateTime.Now.Ticks.ToString() + Path.GetExtension(product.ImageUrl);
                string localFileName = Path.Combine(imagesPath, fileName);
                WebClient webClient = new WebClient();
                webClient.DownloadFile(product.ImageUrl, localFileName);
                return fileName;
            }
            catch (Exception ex)
            {
                LogProvider.SaveLog(LogType.ImageDownloadError, product.SerializeObject(), ex.ToString());
                return string.Empty;
            }
        }

        public override TransactionEvent Add(System.Data.Objects.DataClasses.EntityObject entity)
        {
            Product product = (Product)entity;
            if (product.OldPrice <= product.Price)
            {
                return new TransactionEvent() { IsSuccess = false, ErrorMessage = "Fiyat hatası." };
            }

            return base.Add(entity);
        }

        public override TransactionEvent Update(System.Data.Objects.DataClasses.EntityObject entity)
        {
            Product product = (Product)entity;
            if (product.OldPrice <= product.Price)
            {
                return new TransactionEvent() { IsSuccess = false, ErrorMessage = "Fiyat hatası." };
            }

            return base.Update(entity);
        }
    }
}
